﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Business.Models;
using System.Data;
using System.Data.Entity.Infrastructure;

namespace Business.DAO
{
    public class DaiLyDAO
    {
        private ISSContext db = new ISSContext();
        public List<DaiLy> GetAll()
        {
            return db.DaiLy.ToList();
        }

        public DaiLy Get(int id)
        {
            DaiLy daily = db.DaiLy.Find(id);
            return daily;
        }

        public DaiLy Login(String Name, String Password)
        {
            try
            {
                DaiLy a = db.DaiLy.Where(m => m.TenDaiLy == Name && m.MatKhau == Password).First();
                return a;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<DaiLy> GetByAdminID(Administrators administrators)
        {
            var query = from daily in db.DaiLy where daily.administrator == administrators select daily;
            List<DaiLy> lstDaiLy = query.ToList<DaiLy>();
            return lstDaiLy;
        }

        public int Create(DaiLy daily)
        {
            try
            {
                db.DaiLy.Add(daily);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Edit(DaiLy daily)
        {
            try
            {
                DaiLy tmp = db.DaiLy.Find(daily.IdDaiLy);
                tmp.Email = daily.Email;
                tmp.HoatDong = daily.HoatDong;
                tmp.SoDienThoai = daily.SoDienThoai;
                tmp.TenDaiLy = daily.TenDaiLy;
                DbEntityEntry entry = db.Entry(tmp);
                entry.State = EntityState.Modified;
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int UpdatePassword(DaiLy daily)
        {
            try
            {
                DaiLy tmp = db.DaiLy.Find(daily.IdDaiLy);
                tmp.MatKhau = daily.MatKhau;
                DbEntityEntry entry = db.Entry(tmp);
                entry.State = EntityState.Modified;
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Delete(int id)
        {
            try
            {
                DaiLy daily = db.DaiLy.Find(id);
                db.DaiLy.Remove(daily);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
}